import type { UploadFileInfo } from 'naive-ui'

export function handleDownload(file: UploadFileInfo) {
  if (!file.url) return false

  fetch(file.url)
    .then((response) => response.blob())
    .then((blob) => {
      const url = URL.createObjectURL(blob)

      const link = document.createElement('a')
      link.href = url
      link.download = file.name
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)

      // 清除临时 URL
      URL.revokeObjectURL(url)
    })

  return false
}
